package com.ccl.iot.views;


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class CharSortView extends View {

    private String[] mChars;
    private Paint mNormalP;
    private Paint mSelectP;
    private int mWidth;
    private int mHeight;
    private int mSingleHight;
    private Rect mBounds;

    private String mExtChar;
    private int mIndex = -1;
    private boolean mFromChar = false;

    private CharChangeListener mCharChangeListener;

    public CharSortView(Context context) {
        this(context, null);
    }

    public CharSortView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CharSortView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mChars = new String[]{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q",
                "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};

        mNormalP = new Paint(Paint.ANTI_ALIAS_FLAG);
        mNormalP.setColor(Color.GRAY);
        mNormalP.setTextSize(30);
        mNormalP.setTextAlign(Paint.Align.CENTER);

        mSelectP = new Paint(Paint.ANTI_ALIAS_FLAG);
        mSelectP.setColor(Color.RED);
        mSelectP.setTextSize(30);
        mSelectP.setTextAlign(Paint.Align.CENTER);

        mBounds = new Rect();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth = w;
        mHeight = h;
        mSingleHight = mHeight / mChars.length;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                int dy = (int) event.getY();
                int dindex = dy / mSingleHight;
                if (dindex != mIndex) {
                    mFromChar = true;
                    mIndex = dindex;
                    mExtChar = null;
                    invalidate();
                }
                break;
            case MotionEvent.ACTION_MOVE:
                int my = (int) event.getY();
                int mindex = my / mSingleHight;
                if (mindex != mIndex) {
                    mFromChar = true;
                    mIndex = mindex;
                    mExtChar = null;
                    invalidate();
                }
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                int uy = (int) event.getY();
                int uindex = uy / mSingleHight;
                if (uindex != mIndex) {
                    mFromChar = true;
                    mIndex = uindex;
                    mExtChar = null;
                    invalidate();
                }
                break;
        }

        if(mIndex < 0){
            mIndex = 0;
        }

        if(mIndex >= mChars.length){
            mIndex = mChars.length - 1;
        }
        return true;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        for (int i = 0; i < mChars.length; i++) {
            mNormalP.getTextBounds(mChars[i], 0, 1, mBounds);
            if (mIndex == i || mChars[i].equals(mExtChar)) {
                canvas.drawText(mChars[i], mWidth / 2, mSingleHight / 2 - (mBounds.top - mBounds.bottom) / 2 + mSingleHight * i, mSelectP);
                if (mCharChangeListener != null) {
                    mCharChangeListener.onSelected(mChars[i], mFromChar);
                }
            } else {
                canvas.drawText(mChars[i], mWidth / 2, mSingleHight / 2 - (mBounds.top - mBounds.bottom) / 2 + mSingleHight * i, mNormalP);
            }
        }
    }

    public void setChars(String word) {
        if (!word.equals(mExtChar)) {
            mExtChar = word;
            mIndex = -1;
            mFromChar = false;
            invalidate();
        }
    }

    public void setCharChangeListener(CharChangeListener l) {
        mCharChangeListener = l;
    }

    public interface CharChangeListener {
        public void onSelected(String word, boolean fromChar);
    }
}
